#!/usr/bin/python3
import sys


def get_n(m, ret, n):
    rn = 0
    p = 0
    sr = 0
    for i in range(n):
        while p < n:
            if sr <= m:
                sr += ret[p]
                p += 1
            else:
                break
        sr -= ret_n[i]
        rn += p - i
    return rn


try:
    t = int(input())
    if t < 1 or t > 30:
        sys.exit(0)
    for x in range(t):
        n, k = list(map(int, input().split()))
        s = [int(item) for item in input().split()]
        ret_n = []
        for i in range(1, len(s)):
            if s[i] == s[i - 1]:
                ret_n.append(1)
            else:
                ret_n.append(0)
        ret_n.append(0)
        l = 0
        r = n - 1
        while l < r:
            mid = int((l + r) / 2)
            if get_n(mid, ret_n, n) >= k:
                r = mid
            else:
                l = mid + 1
        print(l)

except ValueError:
    pass
except EOFError:
    pass
except MemoryError:
    pass
